function [ctllist] = ctl_list(filename)
% make a text listing of the control file.
% All entries corresponding to one element of the structure are on one line.
% The first line should contain the names of all of the fields.
% Version 1: fields which are structures are not listed. I will add these later.
%
%

f1=strcat(filename, '.html');
sp_file=strcat(filename, '_sp.html');
iv_file=strcat(filename, '_iv.html');

control = getuserdata; % get the matrix

names=fieldnames(control(1)); % hopefully the first entry is complete.
nfield = size(names);
nentry=length(control);

% we put the table out in HTML format...

ctllist='<html><head>Main</head><body bgcolor="#FFF7DE" link="#247B2B" vlink="#247B2B" ><table border=2 cellspacing=2 cellpadding=2><font face="Arial,Helvetica"><fontsize=-2>';
sp_list='<html><head>Spike</head><body bgcolor="#FFF7DE" link="#247B2B" vlink="#247B2B" ><table border=2 cellspacing=2 cellpadding=2><font face="Arial,Helvetica"><fontsize=-2>';
iv_list='<html><head>IV</head><body bgcolor="#FFF7DE" link="#247B2B" vlink="#247B2B" ><table border=2 cellspacing=2 cellpadding=2><font face="Arial,Helvetica"><fontsize=-2>';
sp_f=1;
iv_f=1;
for SEL=1:nentry
	listinfo='';
	disp(sprintf(':%d',SEL))
	if(SEL==1) % only on the first line, put the names of all the fields
		ctllist=strcat(ctllist,'<tr>');
		for i=1:nfield(1)
			ctllist=strcat(ctllist, sprintf('<td>%s</td>',char(names(i))'));
		end;
		ctllist=strcat(ctllist, '</tr>');
	end;			

	ctllist=strcat(ctllist,'<tr>');
	for i=1:nfield(1)
	   listthis='';
	   thisname = char(names(i));
	   arg = strcat('control(SEL).', thisname);
	   e=eval(char(arg));
	   if(isstruct(e))
	      listthis = '<td>STRUCT</td>';
	   		if(strcmp(names(i), 'spike'))

				sp_list=struct_proc(sp_list, e, control, SEL, sp_f); % rest is the structure information (except substructures)
				if(sp_f)
					sp_f=0;
				end;
			end;
	   		if(strcmp(names(i), 'iv'))
				iv_list=struct_proc(iv_list, e, control, SEL, iv_f); % rest is the structure information (except substructures)
				if(iv_f)
					iv_f=0;
				end;
			end;
	   end
	   if(isnumeric(e))
	      x=eval(char(arg));
		  if(any(size(x)>1))
		  	listthis=sprintf('<td>%s </td>',num2str(x));
		else
			listthis = sprintf('<td>%g</td>', eval(char(arg)));
			end;
	   end
	   if(ischar(e))
	      listthis = sprintf('<td>%s</td>', eval(char(arg)));
	   end
	   if(isempty(e))
	      listthis = sprintf('<td>()</td>');
	   end
	   
	   listinfo = strcat(listinfo, listthis);
	end
	ctllist=strcat(ctllist, [listinfo '</tr>']);

end;
ctllist=strcat(ctllist,'</table></body></html>');
sp_list=strcat(sp_list,'</table></body></html>');
iv_list=strcat(iv_list,'</table></body></html>');

fid=fopen(f1, 'w');
a=size(ctllist)
for i=1:a(1)
	fprintf(fid, '%s', ctllist(i,:));
end;
fclose(fid);
	
fid=fopen(sp_file, 'w');
a=size(sp_list)
for i=1:a(1)
	fprintf(fid, '%s', sp_list(i,:));
end;
fclose(fid);

fid=fopen(iv_file, 'w');
a=size(iv_list)
for i=1:a(1)
	fprintf(fid, '%s', iv_list(i,:));
end;
fclose(fid);


return;


function [list] = struct_proc(list, st, control, SEL, k)
% process a structure and make the table entry for it
if(isempty(st)) % just do nothing if it is an empty structure (variable)
	return;
end;
if(control(SEL).pnd==0 | control(SEL).div==0) % analysis was done, but data is not in the set.
	return;
end;
names=fieldnames(st);
nfield=size(names);
if(k) % only on the first line, put the names of all the fields
	list=strcat(list,'<tr>');
	list=strcat(list, '<td>Filename</td><td>Type</td><td>CA</td>');
	for i=1:nfield(1)
		list=strcat(list, sprintf('<td>%s</td>',char(names(i))'));
	end;
	list=strcat(list, '</tr>');
end;			
list=strcat(list,'<tr>');
list=strcat(list, sprintf('<td>%s</td><td>%g</td><td>%g</td>', control(SEL).filename,...
					control(SEL).pnd, control(SEL).div)); % first entry is filename, pnd and div.
for i=1:nfield(1)
	listthis='';
	arg = sprintf('st.%s',char(names(i)));
	e=eval(char(arg));
	if(isstruct(e))
	   listthis = '<td>STRUCT</td>';
	end
	if(isnumeric(e))
	   x=eval(char(arg));
		if(any(size(x)>1))
		 	listthis=sprintf('<td>%s </td>',num2str(x));
		else
			listthis = sprintf('<td>%g</td>', eval(char(arg)));
		end;
	end
	if(ischar(e))
		listthis = sprintf('<td>%s</td>', eval(char(arg)));
	end
	if(isempty(e))
		listthis = sprintf('<td>()</td>');
	end
	   
	list = strcat(list, listthis);
end
list=[list '</tr>']; % makes the table row...
return;



